﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frm_autenticacao : Form
    {
        public frm_autenticacao()
        {
            InitializeComponent();
        }

        public Utilizador util2;
        int num = 0;

        System.Data.SqlClient.SqlConnection con;


        private void frm_autenticacao_Load(object sender, EventArgs e)
        {
        
        }

        private void cmdentrar_Click(object sender, EventArgs e)
        {
            if (txtuser.Text == "Administrador")
            {
                if (txtpass.Text == "Admin")
                {
                    frm_padmin AdminPainel = new frm_padmin();
                    AdminPainel.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Senha incorreta!", "Verifique a senha", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {


                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                try
                {
                    SqlCommand command1 = con.CreateCommand();
                    command1.CommandText = "SELECT COUNT(*) FROM Utilizador WHERE Login = '" + txtuser.Text + "' AND Pass = '" + txtpass.Text + "'";
                    num = (Int32)command1.ExecuteScalar();

                    if (num == 0)
                    {
                        MessageBox.Show("Dados incorretos!", "Verifique os dados!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        SqlCommand getatrib = con.CreateCommand();
                        getatrib.CommandText = "SELECT * FROM Utilizador WHERE Login = '" + txtuser.Text + "'";
                        SqlDataReader readatrib = getatrib.ExecuteReader();

                        while (readatrib.Read())
                        {
                            util2 = new Utilizador(Convert.ToInt32(readatrib["id_veterinario"].ToString()), readatrib["nome"].ToString(), readatrib["nro_licenca"].ToString(), ((DateTime)readatrib["data_nascimento"]).ToString("dd/MM/yyyy"), readatrib["Login"].ToString(), readatrib["Pass"].ToString());
                        }

                        frm_gesclinica GesCli = new frm_gesclinica(util2);
                        GesCli.Show();
                        this.Hide();
                    }
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.ToString(), "Erro!");
                }

                con.Close();
            }
        }

        private void cmdsair_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
